package ru.arriah.servicedesk.web.dispatcher.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.ClientBean;
import ru.arriah.servicedesk.ejb.ClientManagerLocalHome;
import ru.arriah.servicedesk.ejb.ClientManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;


public class DeleteClientAction extends DumbAction {
   protected ClientManagerLocalHome clientManagerHome;

   public DeleteClientAction() throws NamingException {
      super();

      clientManagerHome =
         Utils.getClientManagerHomeInterface();
   }

   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
     try {              
       String clientIdString = request.getParameter("client_id");
       int clientId = idVal(clientIdString);
       
       ClientManagerLocalObject clientManager = clientManagerHome.create();
       if (clientId!=0) {
         clientManager.deleteClient(clientId);
         addInfMessage(request, config.getProperty("INF.CLIENT.DELETED"));
       } else {
         addErrorMessage(request, config.getProperty("ERROR.CLIENT_NOT_DELETED"));
       }
        
       Collection<ClientBean> clientsList = 
         clientManager.selectClients();
       
       clientManager.remove();
              
       request.setAttribute("clientsList", clientsList);                                  
       return actionMapping.getSuccessTarget();
    } catch (Exception e) {
       handleException(e, "execute");
       throw new InternalException("ErrorMessage.internal");
    }
   }
}